package edu.nf.api.controller.user;

import edu.nf.api.controller.BaseController;
import edu.nf.meituan.model.user.User;
import edu.nf.meituan.service.user.impl.UserServiceImpl;
import edu.nf.util.JwtUtil;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;


/**
 * @Date 2023-06-20
 * @Author qiu
 * 用户登录控制器
 */
public class LoginController extends BaseController {

    /**
     * 用户登录
     * @param userName 账号
     * @param pwd 密码
     * @param session
     * @return
     */
    @RequestMapping("/user/login")
    public View userLogin(@RequestParam("userName") String userName, @RequestParam("pwd") String pwd, HttpSession session) {

        User login = new UserServiceImpl().login(userName, pwd);

        // 转发试图
        //ForwardView forwardView = new ForwardView("index.html");

        // 获取登录信息，存储到 token 中
        HashMap<String, Object> map = new HashMap<>();
        map.put("u_id", login.getUserId());

        // 创建 token
        String token = JwtUtil.createToken(map, 5 * 60 * 1000);

        // 将 token 保存到作用域中
        //session.setAttribute("token",token);

        return new JsonView(success(token));
    }

}
